<?php

App::uses('AppModel', 'Model');
App::uses('File', 'Utility');
App::import('Model', 'Slider');
App::import('Model', 'Product');
App::import('Model', 'Variant');

/**
 * Image Model
 *
 */
class Image extends AppModel {

    var $actsAs = array(
        'MeioUpload.MeioUpload' => array(
            'photo' => array(
                //'removeOriginal' => true,
                'thumbnails' => true,
                'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
                'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'),
                'default' => 'default.jpg'
            ),
        ),
    );

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'photo';

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'order' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'owner_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
    
    var $belongsTo = array(
        'Slider' => array(
            'foreignKey' => 'owner_id',
            'conditions' => array('Image.owner_model' => 'Slider')),
        'Variant' => array(
            'foreignKey' => 'owner_id',
            'conditions' => array('Image.owner_model' => 'Variant')),
        'Gallery' => array(
            'foreignKey' => 'owner_id',
            'conditions' => array('Image.owner_model' => 'Gallery')),
    );

    /**
     * updateMeioUpload: update MeioUpload for object
     *
     * @var null
     */
    public function updateMeioUploadSize($object, $options) {
        $this->Behaviors->unload('MeioUpload.MeioUpload');
        if ($object == "Slider") {
            $width = $options['Slider']['width'];
            $height = $options['Slider']['height'];
            $this->Behaviors->load('MeioUpload.MeioUpload', array('photo' => array(
										//'removeOriginal' => true,
                    'thumbnails' => true,
                    'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
                    'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'),
                    'thumbsizes' => array(
                        'slider' => array('width' => $width,
                            'height' => $height,
                            'forceAspectRatio' => 'C', 'thumbnailQuality' => 100,
                            'zoomCrop' => true),
                        'small' => array('width' => 100,
                            'height' => (100 * $height) / $width,
                            'forceAspectRatio' => 'C', 'thumbnailQuality' => 100,
                            'zoomCrop' => true),
                    ),
                    'default' => 'default.jpg'
            )));
        }
				if($object == "Gallery"){
						$width = 600;
						      $this->Behaviors->load('MeioUpload.MeioUpload', array('photo' => array(
													//'removeOriginal' => true,
						              'thumbnails' => true,
						              'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
						              'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'),
						              'thumbsizes' => array(
						                  'gallery' => array(	'width' => $width,
																									'maxDimension' => 'w',
																									'thumbnailQuality' => 100,
																									'watermark' => 'watermark_150.png'),
						                  'small' => array('width' => 70,
																							 'height' => 70,
																							 'zoomCrop' => true,
																							 'forceAspectRatio' => 'C',
																							 'thumbnailQuality' => 100),
						              ),
						              'default' => 'default.jpg'
						      )));
				}
    }

    /**
     * updateVariantSize
     * @var null
     */
    public function updateVariantSize($object, $sizes) {
        $this->Behaviors->unload('MeioUpload.MeioUpload');

        //get thumbs image for variant from paramenters table
        $thumbs = array();
        foreach ($sizes as $i => $size) {
            $value = explode("x", $size['Parameter']['value']);
            $tmp = array('thumbnailQuality' => 100);

            if (!empty($value[0]) && !empty($value[1])) {
                $tmp['zoomCrop'] = true;
                $tmp['forceAspectRatio'] = 'C';
                $tmp['width'] = intval($value[0]);
                $tmp['height'] = intval($value[1]);
            } elseif (!empty($value[0])) {
                $tmp['width'] = intval($value[0]);
                $tmp['maxDimension'] = 'w';
            } elseif (!empty($value[1])) {
                $tmp['height'] = intval($value[1]);
                $tmp['maxDimension'] = 'h';
            }

            //add watermark
            if (!empty($value[0])) {
                switch (intval($value[0])) {
                    case intval($value[0]) >= 700:
                        $tmp['watermark'] = 'watermark_200.png';
                        break;
                    case intval($value[0]) >= 500:
                        $tmp['watermark'] = 'watermark_150.png';
                        break;
                    case intval($value[0]) >= 300:
                        $tmp['watermark'] = 'watermark_120.png';
                        break;
                    case intval($value[0]) >= 150:
                        $tmp['watermark'] = 'watermark_100.png';
                        break;
                }
            }
            $thumbs[$size['Parameter']['code']] = $tmp;
        }

        $meioUpload = array(
						//'removeOriginal' => true,
            'thumbnails' => true,
            'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
            'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'),
            'default' => 'default.jpg',
            'thumbsizes' => $thumbs
        );

        if ($object == "Variant") {
            $this->Behaviors->load('MeioUpload.MeioUpload', array('photo' => $meioUpload));
        }
    }

    /**
     * getImagesForProduct
     *
     */
    public function getImagesForProduct($productId) {
        $Product = new Product();
        $Variant = new Variant();
        $product = $Product->findById($productId);
        $variantIds = array();
        foreach ($product['Variant'] as $i => $variant) {
            $variantIds[$i] = $variant['id'];
        }
        $images = $this->find('all', array('conditions' => array('owner_model' => $Variant->name,
                'owner_id' => $variantIds)));
        return $images;
    }

}
